<template>
  <div class="home-container">
    <!-- Hero Banner -->
    <section class="hero-banner">
      <div class="banner-content">
        <h1>寻找您的理想座驾</h1>
        <p>上千款优质二手车，品质保障，售后无忧</p>
        <div class="search-box">
          <input type="text" placeholder="输入品牌、型号或关键词" v-model="searchKeyword">
          <button @click="searchCars">搜索</button>
        </div>
      </div>
    </section>

    <!-- Popular Cars Section -->
    <section class="popular-cars">
      <div class="section-header">
        <h2>热门车型</h2>
        <router-link to="/cars" class="view-all">查看全部</router-link>
      </div>
      
      <div class="car-grid">
        <div class="car-card" v-for="car in popularCars" :key="car.id">
          <div class="car-image">
            <img :src="car.imgUrl" :alt="car.title">
            <span class="price">{{ car.sellingPrice }}万</span>
          </div>
          <div class="car-info">
            <h3>{{ car.title }}</h3>
            <p class="details">{{ car.year }} · {{ car.mileage }}万公里 · {{ car.transmission }}</p>
            <div class="tags">
              <span v-for="(tag, index) in car.tags" :key="index">{{ tag }}</span>
            </div>
            <div class="actions">
              <router-link :to="`/car/${car.id}`" class="details-btn">查看详情</router-link>
              <button class="favorite-btn">
                <i class="el-icon-star-off"></i>
              </button>
            </div>
          </div>
        </div>
      </div>
    </section>

    <!-- Car Categories -->
    <section class="car-categories">
      <div class="section-header">
        <h2>按类别浏览</h2>
      </div>
      
      <div class="category-grid">
        <div class="category-card" v-for="category in categories" :key="category.id">
          <router-link :to="category.link">
            <img :src="category.imgUrl" :alt="category.name">
            <h3>{{ category.name }}</h3>
            <p>{{ category.count }}款车型</p>
          </router-link>
        </div>
      </div>
    </section>

    <!-- Why Choose Us -->
    <section class="why-choose-us">
      <div class="section-header">
        <h2>为什么选择我们</h2>
      </div>
      
      <div class="features-grid">
        <div class="feature-card">
          <div class="icon">
            <i class="el-icon-check"></i>
          </div>
          <h3>品质保障</h3>
          <p>所有车辆经过严格检测，确保品质可靠</p>
        </div>
        
        <div class="feature-card">
          <div class="icon">
            <i class="el-icon-money"></i>
          </div>
          <h3>价格透明</h3>
          <p>明码标价，无隐藏费用，支持比价</p>
        </div>
        
        <div class="feature-card">
          <div class="icon">
            <i class="el-icon-service"></i>
          </div>
          <h3>售后保障</h3>
          <p>提供7天无理由退换，30天质保服务</p>
        </div>
        
        <div class="feature-card">
          <div class="icon">
            <i class="el-icon-document"></i>
          </div>
          <h3>手续便捷</h3>
          <p>专业团队协助办理过户，省时省力</p>
        </div>
      </div>
    </section>

    <!-- Testimonials -->
    <section class="testimonials">
      <div class="section-header">
        <h2>客户评价</h2>
      </div>
      
      <div class="testimonial-slider">
        <div class="testimonial-card" v-for="(testimonial, index) in testimonials" :key="index">
          <div class="quote">"</div>
          <p class="content">{{ testimonial.content }}</p>
          <div class="author">
            <img :src="testimonial.avatar" :alt="testimonial.name">
            <div class="author-info">
              <h4>{{ testimonial.name }}</h4>
              <p>{{ testimonial.city }}</p>
            </div>
          </div>
        </div>
      </div>
    </section>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()
const searchKeyword = ref('')

// 搜索车辆
function searchCars() {
  if (searchKeyword.value.trim()) {
    router.push({
      path: '/cars',
      query: { keyword: searchKeyword.value }
    })
  }
}

// 热门车型数据
const popularCars = ref([])
const loading = ref(true)
const apiUrl = 'http://localhost:9999/api/cars'

// 获取热门车型数据
const fetchPopularCars = async () => {
  loading.value = true
  try {
    console.log('开始获取热门车型数据')
    const response = await fetch(apiUrl)
    if (!response.ok) {
      throw new Error(`HTTP error! Status: ${response.status}`)
    }
    const data = await response.json()
    console.log('获取到的热门车型数据:', data)
    
    // 转换后端数据为前端所需格式
    if (data && data.length > 0) {
      popularCars.value = data.slice(0, 8).map(car => {
        return {
          id: car.carId || car.id,
          title: `${car.brand} ${car.carType || car.model || ''}`,
          year: car.produceTime ? new Date(car.produceTime).getFullYear() : '2023',
          mileage: (Math.random() * 5 + 1).toFixed(1),
          transmission: Math.random() > 0.5 ? '自动' : '手动',
          sellingPrice: (car.guidePrice / 10000).toFixed(2),
          imgUrl: car.image || 'https://via.placeholder.com/300x200?text=CarShop',
          tags: [car.carType || '新车', car.brand, '优选车源']
        }
      })
    } else {
      loadMockCars()
    }
  } catch (error) {
    console.error('获取热门车型失败:', error)
    loadMockCars()
  } finally {
    loading.value = false
  }
}

// 加载模拟数据
const loadMockCars = () => {
  console.log('加载模拟车辆数据')
  popularCars.value = [
    {
      id: 1,
      title: '特斯拉 Model 3',
      year: '2022',
      mileage: '1.2',
      transmission: '自动',
      sellingPrice: '28.99',
      imgUrl: 'https://via.placeholder.com/300x200?text=Tesla+Model+3',
      tags: ['新能源', '自动驾驶', '优选车源']
    },
    {
      id: 2,
      title: '比亚迪 汉',
      year: '2023',
      mileage: '0.8',
      transmission: '自动',
      sellingPrice: '21.58',
      imgUrl: 'https://via.placeholder.com/300x200?text=BYD+Han',
      tags: ['新能源', '豪华车', '优选车源']
    },
    {
      id: 3,
      title: '奥迪 A6L',
      year: '2021',
      mileage: '3.5',
      transmission: '自动',
      sellingPrice: '42.68',
      imgUrl: 'https://via.placeholder.com/300x200?text=Audi+A6L',
      tags: ['豪华车', '舒适', '优选车源']
    },
    {
      id: 4,
      title: '宝马 5系',
      year: '2022',
      mileage: '2.3',
      transmission: '自动',
      sellingPrice: '46.99',
      imgUrl: 'https://via.placeholder.com/300x200?text=BMW+5',
      tags: ['豪华车', '运动', '优选车源']
    },
    {
      id: 5,
      title: '小鹏 P7',
      year: '2023',
      mileage: '0.6',
      transmission: '自动',
      sellingPrice: '24.99',
      imgUrl: 'https://via.placeholder.com/300x200?text=Xpeng+P7',
      tags: ['新能源', '自动驾驶', '优选车源']
    },
    {
      id: 6,
      title: '理想 L9',
      year: '2023',
      mileage: '1.0',
      transmission: '自动',
      sellingPrice: '45.98',
      imgUrl: 'https://via.placeholder.com/300x200?text=LI+L9',
      tags: ['新能源', 'SUV', '优选车源']
    },
    {
      id: 7,
      title: '蔚来 ET7',
      year: '2023',
      mileage: '0.9',
      transmission: '自动',
      sellingPrice: '35.88',
      imgUrl: 'https://via.placeholder.com/300x200?text=NIO+ET7',
      tags: ['新能源', '豪华车', '优选车源']
    },
    {
      id: 8,
      title: '大众 途观L',
      year: '2022',
      mileage: '1.8',
      transmission: '自动',
      sellingPrice: '22.98',
      imgUrl: 'https://via.placeholder.com/300x200?text=VW+Tiguan',
      tags: ['SUV', '家用', '优选车源']
    }
  ]
}

// 在组件挂载时获取数据
onMounted(() => {
  fetchPopularCars()
})

// 车辆类别
const categories = ref([
  {
    id: 1,
    name: '轿车',
    count: 325,
    imgUrl: 'https://via.placeholder.com/200x150',
    link: '/cars?category=sedan'
  },
  {
    id: 2,
    name: 'SUV',
    count: 207,
    imgUrl: 'https://via.placeholder.com/200x150',
    link: '/cars?category=suv'
  },
  {
    id: 3,
    name: '新能源',
    count: 128,
    imgUrl: 'https://via.placeholder.com/200x150',
    link: '/cars?category=new-energy'
  },
  {
    id: 4,
    name: '豪华车',
    count: 156,
    imgUrl: 'https://via.placeholder.com/200x150',
    link: '/cars?category=luxury'
  }
])

// 客户评价
const testimonials = ref([
  {
    name: '张先生',
    city: '上海',
    content: '在CarShop购买了一辆宝马3系，整个过程非常顺畅，服务也很周到。车况比我想象的还要好，价格也很合理。',
    avatar: 'https://via.placeholder.com/50x50'
  },
  {
    name: '李女士',
    city: '北京',
    content: '作为女性购车者，在这里买车感到很安心。销售不会像4S店那样强行推销，而是根据我的需求推荐合适的车型。',
    avatar: 'https://via.placeholder.com/50x50'
  },
  {
    name: '王先生',
    city: '广州',
    content: '已经是第二次在CarShop购车了，售后服务做得很好。车辆检测也很严格，买到的车都没有什么问题。',
    avatar: 'https://via.placeholder.com/50x50'
  }
])
</script>

<style lang="scss" scoped>
.home-container {
  width: 100%;
}

// Hero Banner
.hero-banner {
  width: 100%;
  height: 500px;
  background-image: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)), url('https://via.placeholder.com/1920x500');
  background-size: cover;
  background-position: center;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  text-align: center;
  margin: 0 0 50px 0;
  
  .banner-content {
    max-width: 800px;
    padding: 0 20px;
    
    h1 {
      font-size: 48px;
      margin-bottom: 20px;
    }
    
    p {
      font-size: 20px;
      margin-bottom: 30px;
    }
    
    .search-box {
      display: flex;
      max-width: 600px;
      margin: 0 auto;
      
      input {
        flex: 1;
        padding: 15px 20px;
        border: none;
        border-radius: 4px 0 0 4px;
        font-size: 16px;
      }
      
      button {
        padding: 15px 30px;
        background-color: #409EFF;
        color: white;
        border: none;
        border-radius: 0 4px 4px 0;
        font-size: 16px;
        cursor: pointer;
        transition: background-color 0.3s;
        
        &:hover {
          background-color: #66b1ff;
        }
      }
    }
  }
}

// Section Styles
.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30px;
  
  h2 {
    font-size: 28px;
    color: #333;
  }
  
  .view-all {
    color: #409EFF;
    text-decoration: none;
    font-weight: 500;
    
    &:hover {
      text-decoration: underline;
    }
  }
}

// Common section styles
.popular-cars, .car-categories, .why-choose-us, .testimonials {
  max-width: 1400px;
  margin: 0 auto 60px;
  padding: 0 20px;
}

// Popular Cars
.popular-cars {
  margin-bottom: 60px;
  
  .car-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
    gap: 25px;
  }
  
  .car-card {
    background-color: white;
    border-radius: 8px;
    overflow: hidden;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    transition: transform 0.3s, box-shadow 0.3s;
    
    &:hover {
      transform: translateY(-5px);
      box-shadow: 0 10px 20px rgba(0, 0, 0, 0.15);
    }
    
    .car-image {
      position: relative;
      height: 180px;
      
      img {
        width: 100%;
        height: 100%;
        object-fit: cover;
      }
      
      .price {
        position: absolute;
        bottom: 10px;
        right: 10px;
        background-color: rgba(0, 0, 0, 0.7);
        color: white;
        padding: 5px 10px;
        border-radius: 4px;
        font-weight: bold;
      }
    }
    
    .car-info {
      padding: 15px;
      
      h3 {
        margin: 0 0 10px;
        font-size: 18px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }
      
      .details {
        color: #666;
        margin-bottom: 10px;
      }
      
      .tags {
        display: flex;
        flex-wrap: wrap;
        gap: 8px;
        margin-bottom: 15px;
        
        span {
          background-color: #f0f7ff;
          color: #409EFF;
          padding: 4px 8px;
          border-radius: 4px;
          font-size: 12px;
        }
      }
      
      .actions {
        display: flex;
        justify-content: space-between;
        align-items: center;
        
        .details-btn {
          background-color: #409EFF;
          color: white;
          text-decoration: none;
          padding: 8px 15px;
          border-radius: 4px;
          flex: 1;
          text-align: center;
          margin-right: 10px;
          
          &:hover {
            background-color: #66b1ff;
          }
        }
        
        .favorite-btn {
          background-color: white;
          border: 1px solid #dcdfe6;
          color: #909399;
          padding: 8px;
          border-radius: 4px;
          cursor: pointer;
          
          &:hover {
            color: #f56c6c;
            border-color: #f56c6c;
          }
        }
      }
    }
  }
}

// Car Categories
.car-categories {
  margin-bottom: 60px;
  
  .category-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
    gap: 20px;
  }
  
  .category-card {
    background-color: white;
    border-radius: 8px;
    overflow: hidden;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    transition: transform 0.3s;
    
    &:hover {
      transform: translateY(-5px);
    }
    
    a {
      display: block;
      text-decoration: none;
      color: inherit;
    }
    
    img {
      width: 100%;
      height: 140px;
      object-fit: cover;
    }
    
    h3 {
      margin: 15px 15px 5px;
      font-size: 18px;
    }
    
    p {
      margin: 0 15px 15px;
      color: #666;
    }
  }
}

// Why Choose Us
.why-choose-us {
  margin-bottom: 60px;
  background-color: #f9f9f9;
  padding: 50px 20px;
  
  .features-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
    gap: 30px;
  }
  
  .feature-card {
    background-color: white;
    border-radius: 8px;
    padding: 30px;
    text-align: center;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
    
    .icon {
      width: 70px;
      height: 70px;
      background-color: #f0f7ff;
      border-radius: 50%;
      display: flex;
      align-items: center;
      justify-content: center;
      margin: 0 auto 20px;
      color: #409EFF;
      font-size: 30px;
    }
    
    h3 {
      margin-bottom: 15px;
      font-size: 20px;
    }
    
    p {
      color: #666;
      line-height: 1.6;
    }
  }
}

// Testimonials
.testimonials {
  margin-bottom: 60px;
  
  .testimonial-slider {
    display: flex;
    gap: 30px;
    overflow-x: auto;
    padding: 10px 0 30px;
    
    &::-webkit-scrollbar {
      height: 8px;
    }
    
    &::-webkit-scrollbar-track {
      background: #f1f1f1;
      border-radius: 4px;
    }
    
    &::-webkit-scrollbar-thumb {
      background: #ccc;
      border-radius: 4px;
    }
  }
  
  .testimonial-card {
    flex: 0 0 350px;
    background-color: white;
    border-radius: 8px;
    padding: 30px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    position: relative;
    
    .quote {
      font-size: 60px;
      line-height: 1;
      color: #e1e1e1;
      position: absolute;
      top: 20px;
      left: 20px;
    }
    
    .content {
      margin-bottom: 20px;
      line-height: 1.6;
      color: #666;
      position: relative;
      z-index: 1;
    }
    
    .author {
      display: flex;
      align-items: center;
      
      img {
        width: 50px;
        height: 50px;
        border-radius: 50%;
        margin-right: 15px;
      }
      
      .author-info {
        h4 {
          margin: 0 0 5px;
        }
        
        p {
          margin: 0;
          color: #666;
        }
      }
    }
  }
}

// Responsive Adjustments
@media (max-width: 768px) {
  .hero-banner {
    height: 400px;
    
    .banner-content {
      h1 {
        font-size: 36px;
      }
      
      p {
        font-size: 18px;
      }
    }
  }
  
  .section-header h2 {
    font-size: 24px;
  }
  
  .testimonial-slider .testimonial-card {
    flex: 0 0 300px;
  }
}
</style> 